﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using NIEN_LUAN.Db;
using System.Windows.Forms;

namespace NIEN_LUAN
{
    public partial class FormTimChiTietMatHang : Form
    {
        private DataContentDataContext db = new DataContentDataContext();
        private FormLapChungTu frmLapChungTu;
        private FormLapPhieuNhap frmLapPhieuNhap;
        private FormChiTietChungTu frmChiTietChungTu;
        public MATHANG MatHangadd = new MATHANG();

        public FormTimChiTietMatHang()
        {
            InitializeComponent();            
        }
        
        public FormTimChiTietMatHang(FormLapChungTu frm)
        {
            this.frmLapChungTu = frm;
            InitializeComponent();

            //this.btnTimMH.Enabled = true;
            //this.btnChon.Enabled = false;
            //this.btnAddMH.Enabled = false;
            //this.btnXoaMH.Enabled = false;
            //this.btnHuyMH.Enabled = false;
            //this.btnThoat.Enabled = true;
            this.btnThemMH.Enabled = false;
        }
        public FormTimChiTietMatHang(FormChiTietChungTu frm)
        {
            InitializeComponent();
            this.frmChiTietChungTu = frm;
            this.btnThemMH.Visible = false;
            
        }
        public FormTimChiTietMatHang(FormLapPhieuNhap frm)
        {
            this.frmLapPhieuNhap = frm;
            InitializeComponent();
            this.btnThemMH.Visible = true;

            //this.btnTimMH.Enabled = true;
            //this.btnChon.Enabled = false;
            //this.btnAddMH.Enabled = false;
            //this.btnXoaMH.Enabled = false;
            //this.btnHuyMH.Enabled = false;
            //this.btnThoat.Enabled = true;
            //this.btnThemMH.Enabled = false;
        }
        
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;//khi click vao day thi tra ve dialog result la cancel
            this.Close();
        }

        private void btnHuyMH_Click(object sender, EventArgs e)
        {
            this.txtTenHang.ResetText();
            this.cmbLoaiHang.ResetText();
            this.cmbLoaiHang.Focus();
            this.dataGridViewDSLHThem.Rows.Clear();
        }

        private void btnTimMH_Click(object sender, EventArgs e)
        {          
                var hangs = (from m in db.MATHANGs
                             where m.TenHang.StartsWith(this.txtTenHang.Text)
                             select m).ToList();
                this.mATHANGBindingSource.DataSource = hangs;
               
            //if (frmLapPhieuNhap!=null)
            //{
            //    this.btnTimMH.Enabled = true;
            //    this.btnChon.Enabled = true;
            //    this.btnAddMH.Enabled = true;
            //    this.btnXoaMH.Enabled = true;
            //    this.btnHuyMH.Enabled = true;
            //    this.btnThoat.Enabled = true;
            //    this.btnThemMH.Enabled = true;
            //}           
            
            //if (frmLapChungTu!=null)
            //{
            //    this.btnTimMH.Enabled = true;
            //    this.btnChon.Enabled = true;
            //    this.btnAddMH.Enabled = true;
            //    this.btnXoaMH.Enabled = true;
            //    this.btnHuyMH.Enabled = true;
            //    this.btnThoat.Enabled = true;
            //    this.btnThemMH.Enabled = false;
            //}
         }

        private void FormTimChiTietMatHang_Load(object sender, EventArgs e)
        {
            this.dataGridViewDSLH.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridViewDSLHThem.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridViewDSLH.AutoGenerateColumns = false;
            var mathang = (from lh in db.MATHANGs
                            orderby lh.MaHang ascending
                            select lh).ToList();
            this.mATHANGBindingSource.DataSource = mathang;

            var loaihangs = (from m in db.LOAIHANGs
                             select m).ToList();
            this.bsLoaiHang.DataSource = loaihangs;
            this.cmbLoaiHang.DataSource = bsLoaiHang;
           // this.cmbLoaiHang.DisplayMember = "MaLoaiHang";
            this.cmbLoaiHang.DisplayMember = "TenLoaiHang";
            if (frmChiTietChungTu != null)
            {
                var listXuat=frmChiTietChungTu.bsChiTietChungTu.DataSource as List<ChitietNhapXuat>;
                this.bsMatHangChon.DataSource = listXuat;
            }
            this.dataGridViewDSLHThem.DataSource = bsMatHangChon;           
        }

        private void bsLoaiHang_CurrentChanged(object sender, EventArgs e)
        {
            var loaihang = this.bsLoaiHang.Current as LOAIHANG;
            if (loaihang != null)
            {
                var hang =( from m in db.MATHANGs
                           where m.MaLoaiHang == loaihang.MaLoaiHang
                           select m).ToList();
                this.mATHANGBindingSource.DataSource = hang;
            }
        }       
      
        private void btnAddMH_Click(object sender, EventArgs e)
        {
            if (this.frmLapChungTu != null)
            {
                var tongtien = danhsachchitietchungtu.Sum(m => m.SoLuong * m.DonGia);
                this.frmLapChungTu.cHITIETCHUNGTUBindingSource.DataSource = this.bsMatHangChon.DataSource;
                this.frmLapChungTu.cHITIETCHUNGTUBindingSource.ResetBindings(true);
                this.frmLapChungTu.numericUDTongtien.Value = tongtien;
                this.DialogResult = DialogResult.OK;
            }

            if (this.frmLapPhieuNhap != null)
            {
                var tongtien = danhsachchitietphieunhap.Sum(m => m.SoLuong * m.DonGia);
                this.frmLapPhieuNhap.cHITIETPHIEUNHAPBindingSource.DataSource = this.bsMatHangChon.DataSource;
                this.frmLapPhieuNhap.cHITIETPHIEUNHAPBindingSource.ResetBindings(true);
                this.frmLapPhieuNhap.numericUDTongtien.Value = tongtien;
                this.DialogResult = DialogResult.OK;
            }
            if (this.frmChiTietChungTu != null)
            {
                var tongtien = danhsachHangNhap.Sum(m => m.SoLuong * m.DonGia);
                this.frmChiTietChungTu.numericUDTongitien.Value = tongtien;
                this.frmChiTietChungTu.bsChiTietChungTu.DataSource = this.bsMatHangChon.DataSource;
                this.frmChiTietChungTu.bsChiTietChungTu.ResetBindings(true);
                this.DialogResult = DialogResult.OK;
            }
        }

        private List<ChitietNhapXuat> danhsachchitietchungtu = new List<ChitietNhapXuat>();
        private List<ChitietNhapXuat> danhsachchitietphieunhap = new List<ChitietNhapXuat>();
        private List<ChitietNhapXuat> danhsachHangNhap = new List<ChitietNhapXuat>();
        private void btnChon_Click(object sender, EventArgs e)
        { 
            var hangs = this.mATHANGBindingSource.Current as MATHANG;

            #region Nếu Là Lập chứng từ
            if (frmLapChungTu != null)
            {
                if (hangs != null)
                {
                    danhsachchitietchungtu = (List<ChitietNhapXuat>)this.bsMatHangChon.DataSource ?? new List<ChitietNhapXuat>();
                    var hangdangchon = (from m in danhsachchitietchungtu
                                        where m.MaHang == hangs.MaHang
                                        select m
                                        ).ToList();
                    //n^ếu đ~a chọn

                    if (hangdangchon.Count() > 0)
                    {
                        var hangchontmp = hangdangchon.First();
                        for (int i = 0; i < danhsachchitietchungtu.Count; i++)
                        {
                            if (danhsachchitietchungtu[i].MaHang == hangchontmp.MaHang)
                            {
                                danhsachchitietchungtu[i].SoLuong++;

                                break;
                            }
                        }
                    }
                    else
                    {
                        bsMatHangChon.ResetCurrentItem();
                        //this.bsMatHangChon.Add(new CHITIETCHUNGTU
                        //{
                        //    DonGiaXuat = hangs.GiaBan,
                        //    MaHang = hangs.MaHang,
                        //    SoLuongXuat = 1,
                        //    MATHANG = hangs
                        //});
                        danhsachchitietchungtu.Add(new ChitietNhapXuat
                        {
                            DonGia = hangs.GiaBan ?? 0,
                            MaHang = hangs.MaHang,
                            SoLuong = 1,
                            TenHang = hangs.TenHang
                            //DonGiaXuat = hangs.GiaBan,
                            //MaHang = hangs.MaHang,
                            //SoLuongXuat = 1,
                            //MATHANG = hangs
                        });
                    }
                    this.bsMatHangChon.DataSource = danhsachchitietchungtu;
                    bsMatHangChon.ResetBindings(true);
                    // this.dataGridViewDSLHThem.DataSource = bsMatHangChon;
                }
            } 
            #endregion
            
            //add v`ao lưới dưới kia           
            #region Nếu làm phiếu nhập
            if (frmLapPhieuNhap != null)
            {
                if (hangs != null)
                {
                    var hangdangchon = (from m in danhsachchitietphieunhap
                                        where m.MaHang == hangs.MaHang
                                        select m).ToList();
                    //n^ếu đ~a chọn
                    if (hangdangchon.Count() > 0)
                    {
                        var hangchontmp = hangdangchon.First();
                        for (int i = 0; i < danhsachchitietphieunhap.Count; i++)
                        {
                            if (danhsachchitietphieunhap[i].MaHang == hangchontmp.MaHang)
                            {
                                danhsachchitietphieunhap[i].SoLuong++;
                                break;
                            }
                        }
                    }
                    else
                    {
                        bsMatHangChon.ResetCurrentItem();
                        //this.bsMatHangChon.Add(new CHITIETPHIEUNHAP
                        //{
                        //    DonGiaNhap = hangs.GiaBan??0,
                        //    MaHang = hangs.MaHang,
                        //    SoLuongNhap = 1,
                        //    MATHANG = hangs
                        //});
                        danhsachchitietphieunhap.Add(new ChitietNhapXuat
                        {
                            DonGia = hangs.GiaNhap ?? 0,
                            MaHang = hangs.MaHang,
                            SoLuong = 1,
                            TenHang = hangs.TenHang
                            //DonGiaNhap = hangs.GiaBan,
                            //MaHang = hangs.MaHang,
                            //SoLuongNhap = 1,
                            //MATHANG = hangs
                        });
                    }
                    this.bsMatHangChon.DataSource = danhsachchitietphieunhap;
                    bsMatHangChon.ResetBindings(true);
                   // this.dataGridViewDSLHThem.DataSource = bsMatHangChon;
                }
            } 
            #endregion


            #region Nếu là phiếu chi tiết chứng từ


            if (this.frmChiTietChungTu != null)
            {
                danhsachHangNhap = this.bsMatHangChon.DataSource as List<ChitietNhapXuat>;
                if (hangs != null)
                {
                    var hangdangchon = (from m in danhsachchitietphieunhap
                                        where m.MaHang == hangs.MaHang
                                        select m).ToList();
                    //n^ếu đ~a chọn
                    if (hangdangchon.Count() > 0)
                    {
                        var hangchontmp = hangdangchon.First();
                        for (int i = 0; i < danhsachchitietphieunhap.Count; i++)
                        {
                            if (danhsachchitietphieunhap[i].MaHang == hangchontmp.MaHang)
                            {
                                danhsachchitietphieunhap[i].SoLuong++;
                                break;
                            }
                        }
                    }
                    else
                    {
                        bsMatHangChon.ResetCurrentItem();
                        //this.bsMatHangChon.Add(new CHITIETPHIEUNHAP
                        //{
                        //    DonGiaNhap = hangs.GiaBan??0,
                        //    MaHang = hangs.MaHang,
                        //    SoLuongNhap = 1,
                        //    MATHANG = hangs
                        //});
                        danhsachchitietphieunhap.Add(new ChitietNhapXuat
                        {
                            DonGia = hangs.GiaNhap ?? 0,
                            MaHang = hangs.MaHang,
                            SoLuong = 1,
                            TenHang = hangs.TenHang
                            //DonGiaNhap = hangs.GiaBan,
                            //MaHang = hangs.MaHang,
                            //SoLuongNhap = 1,
                            //MATHANG = hangs
                        });
                    }
                    this.bsMatHangChon.DataSource = danhsachchitietphieunhap;
                    bsMatHangChon.ResetBindings(true);
                    // this.dataGridViewDSLHThem.DataSource = bsMatHangChon;
                }
            } 


            #endregion
        }

        private void btnThemMH_Click(object sender, EventArgs e)
        {
            Form frmThemCTMH = new FormChiTietMatHang(this);
            var kq = frmThemCTMH.ShowDialog();
            
            if (kq == DialogResult.OK)
            {
                var loaihang = (from m in db.LOAIHANGs
                               select m).ToList();
                bsLoaiHang.DataSource = loaihang;
                bsLoaiHang.ResetBindings(true);
              
                MessageBox.Show("Thêm thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                this.bsLoaiHang.MoveLast();
            }
        }

        private void btnXoaMH_Click(object sender, EventArgs e)
        {
            var mathang = this.mATHANGBindingSource.Current as MATHANG;
            if (mathang!=null)
            {
                this.bsMatHangChon.RemoveCurrent();            
            }
            else 
            {
                MessageBox.Show("Bạn chưa chọn hàng","Thông báo",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                return;
            }
        }           
    }
}
